﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BitcoinCommon;
using BitcoinMarket.Data;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;


namespace BitcoinMarket.Market
{
    public class KorbitMarketPrice
    {
        MainInfo Comm; 
        public DateTime OrderTime;
        public DataMarketOpen OpenAsk;
        public DataMarketOpen OpenBid;

        private enum eOrderTable
        {
            ASK,
            BID
        }

        public KorbitMarketPrice(MainInfo objComm)
        {
            Comm = objComm;
        }

        public DataMarketPrice MarketPrice()
        {
            DataMarketPrice objReturn = new DataMarketPrice();

            //Common을 통해서 데이터를 조회한다.
            string strURL = "";
            string strParameters = "";
            string strData = "";

            strURL = Comm.GetKorbitURL() + StringURL.URL_KORBIT_LAST_CONTRACT;
            strParameters = "";

            strData = Comm.RequestData(strURL, strParameters);

            if (strData.Length == 0)
                return null;

            try
            {
                JObject oitem = JObject.Parse(strData);

                objReturn.LastTime = DateTime.Now;
                objReturn.LastPrice = Double.Parse(oitem["last"].ToString());
                objReturn.BidPrice = Double.Parse(oitem["bid"].ToString());
                objReturn.AskPrice = Double.Parse(oitem["ask"].ToString());
                objReturn.LowPrice = Double.Parse(oitem["low"].ToString());
                objReturn.HighPrice = Double.Parse(oitem["high"].ToString());
                objReturn.Volume = Double.Parse(oitem["volume"].ToString());
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                return null;
            }
            
            return objReturn;

        }

        public void RetrivedOpenOrder()
        {
            //Common을 통해서 데이터를 조회한다.
            string strURL = "";
            string strParameters = "";
            string strData = "";

            strURL = Comm.GetKorbitURL() + StringURL.URL_KORBIT_ORDER_BOOKS;
            strParameters = "";

            strData = Comm.RequestData(strURL, strParameters);

            if (strData.Length == 0)
                return;

            ConvertToOrders(strData);

        }

        private void ConvertToOrders(string strData)
        {
            if (strData.Length == 0)
                return;

            //데이터를 추출
            JObject oData = JObject.Parse(strData);

            try
            {
                JArray oAsks = (JArray)oData["asks"];
                ConvertToOrderTable(oAsks, eOrderTable.ASK);

                JArray oBids = (JArray)oData["bids"];
                ConvertToOrderTable(oBids, eOrderTable.BID);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
            
            OrderTime = DateTime.Now;
        }

        private void ConvertToOrderTable(JArray objData, eOrderTable eOrder)
        {
            double dbBTCPrice;
            double dbBTCVolume;

            //없는 경우 내보내기
            if (objData == null)
                return;

            //데이터를 초기화하고
            DataMarketOpen objTable = new DataMarketOpen();

            //테이블 데이터를 비운다.
            objTable.Clear();

            try
            {
                foreach (JToken oItem in objData)
                {
                    try
                    {
                        dbBTCPrice = Double.Parse(oItem[0].ToString());
                        dbBTCVolume = Double.Parse(oItem[1].ToString());
                        objTable.AddRow(dbBTCPrice, dbBTCVolume);
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e.Message);
                    }

                }

                //데이터가 없으면 바인딩하지 않는다.
                if (objTable.RowCount() == 0)
                    return;

                //데이터를 삽입한다.
                if (eOrder == eOrderTable.ASK)
                    OpenAsk = objTable;
                else
                    OpenBid = objTable;

            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }

    }
}
